<?php
namespace Api;
use Kuyuan\WqCore\api\Api;
use Kuyuan\WqCore\util\Util;

/**
 * 系统管理员
 * Class Link
 * @package Api
 */
class Manage extends Api
{
    private $Manage = null;

    public function __construct()
    {
        parent::__construct();
        $this->Manage = new \Controller\Manage();
    }

    /**
     * 新增修改管理员
     */
    public function handle()
    {
        Util::appointPost();
        $_returns = array("code" => 0);
        try {
            if (!$this->_gpc["manage_id"] && $this->Manage->testManage($this->_gpc["data"]["uid"])) {
                throw new \Exception("当前用户已是管理员", 103);
            }
            if (!$this->Manage->handle($this->_gpc["data"])) {
                throw new \Exception("操作失败", 103);
            }
        } catch (\Exception $e) {
            $_returns = array("code" => $e->getCode(), "err_msg" => $e->getMessage());
        }
        Util::returns($_returns);
    }

    /**
     * 获取列表
     */
    public function getList()
    {
        Util::appointGet();
        $_list = $this->Manage->getList();
        $_returns = array("code"=>101);
        if(count($_list) > 0) {
            $Member = $this->newController("Member");
            foreach($_list as $_k=>$_v) {
                $_member = $Member->findNameHeader($_v["uid"]);
                $_list[$_k]["nickname"] = $_member["nickname"];
                $_list[$_k]["avatar"] = $_member["avatar"];
            }
            unset($Member,$_member);
            $_returns = array("code"=>0,"data"=>$_list);
        }
        Util::returns($_returns);
    }

    /**
     * 移除管理员
     */
    public function remove()
    {
        Util::appointPost();
        $_returns = array("code"=>0);
        if(!$this->Manage->remove($this->_gpc["manage_id"])) {
            $_returns = array("code"=>102);
        }
        Util::returns($_returns);
    }
}